package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/7 9:25
 * @description
 */
public class IODemo {

    public static void fisDemo(){
        try {
            FileInputStream fis = new FileInputStream("D:\\file\\test\\0001.txt");


            //实际读入的字节数
            int i =  fis.read();

            String.valueOf(i);
            System.out.println((char) i);

            fis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void fisDemo2(){
        try {
            FileInputStream fis = new FileInputStream("D:\\file\\test\\0001.txt");

            //读取文件 将读取的文件存入字节数组
            byte[] buff = new byte[1024];
            //实际读入的字节数
            int i =  fis.read(buff);

            String.valueOf(i);
            System.out.println(new String(buff,0,i));

            fis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void fisDemo3(){
        //文件比较大一次性无法读入
        //循环读取
        //文件结束标识 -1 规定
        try {
            //创建文件输入流
            FileInputStream fis = new FileInputStream("D:\\file\\test\\0001.txt");

            //读取文件 将读取的文件存入字节数组
            byte[] buff = new byte[1024];
            //实际读入的字节数
            int len = 0;//记录每次读取的字节数
            //每次读取字节填充到数组中，len代表每次读入的字节数
            //len=-1时代表文件已经到末尾，跳出循环
            while ((len = fis.read(buff))!=-1){
                //输出
                System.out.println(new String(buff,0,len));
            }

            fis.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        fisDemo3();
    }
}
